package com.sphd.emanu.view.shade;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import com.sphd.emanu.R;

/**
 * @Author Hu
 * @Time 2017/7/7 --11:36.
 * @Email 87683202@qq.com
 */
public class FanImageView extends View {
    private Paint backPaint;//画笔

    private float sqrt2 = (float) Math.sqrt(2);

    private int bgColor = Color.BLUE;

    private int radius = 10;

    private Path path;//划区域

    private int minWidth;

    private int resImage=0;//背景图片




    public int getRadius() {
        return radius;
    }

    public void setRadius(int radius) {
        this.radius = radius;
    }

    public FanImageView(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray typedArray= context.getTheme().obtainStyledAttributes(attrs, R.styleable.FanImageView,0,0);
        bgColor=typedArray.getColor(R.styleable.FanImageView_fanbackground,bgColor);
        resImage=typedArray.getResourceId(R.styleable.FanImageView_imageres,resImage);
        backPaint = new Paint();
        backPaint.setAntiAlias(true);
        backPaint.setStyle(Paint.Style.FILL);
        backPaint.setColor(bgColor);
        path = new Path();


    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        path.reset();
        path.moveTo(getWidth() - getRadius(), Math.max(minWidth,getHeight()));
        path.quadTo(getWidth(), Math.max(minWidth,getHeight()),
                getWidth(), Math.max(minWidth,getHeight()) - getRadius());
        path.lineTo(getWidth(), minWidth==getHeight()?0:getHeight()-minWidth);

        path.quadTo(getWidth()-Math.max(minWidth,getHeight()-minWidth),
                minWidth==getHeight()?0:getHeight()-minWidth,
                getWidth()-Math.max(minWidth,getHeight()-minWidth),
                Math.max(minWidth,getHeight()));
        path.lineTo(getWidth() - getRadius(), Math.max(minWidth,getHeight()));
        canvas.drawPath(path, backPaint);

        Bitmap bmp= BitmapFactory.decodeResource(getResources(), resImage);

        canvas.drawBitmap(bmp,getWidth()-Math.max(minWidth,getHeight()-minWidth)+(minWidth/(2*sqrt2)),
                (minWidth==getHeight()?0:getHeight()-minWidth)+(minWidth/(2*sqrt2)),backPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
//        Log.e("大小","widthSize=="+widthSize+
//                "heightSize"+heightSize+
//                "比较结果==="+Math.min( widthSize, heightSize));
        minWidth = Math.min( widthSize, heightSize);

        setMeasuredDimension(widthSize, heightSize);

    }
}
